package com.gasstation

import com.gasstation.domain.GasSummary
import com.gasstation.domain.GasUtil

class GasSummaryController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		GasSummary gasSummary = new GasSummary()

		Date month = params.monthYear
		if(month == null){
			month = new Date()
		}
		def regularInventoryCriteria = InventoryRegular.createCriteria()
		List<InventoryRegular> regularInvList = regularInventoryCriteria.list{
			between("transactionOn", GasUtil.getFirstDayOfMonth(month), GasUtil.getLastDayOfMonth(month))
			order("transactionOn", "desc")
		}
		def midGradeInventoryCriteria = InventoryMidGrade.createCriteria()
		List<InventoryMidGrade> midGradeInvList = midGradeInventoryCriteria.list{
			between("transactionOn", GasUtil.getFirstDayOfMonth(month), GasUtil.getLastDayOfMonth(month))
			order("transactionOn", "desc")
		}

		def premiumInventoryCriteria = InventoryPremium.createCriteria()
		List<InventoryPremium> premiumInvList = premiumInventoryCriteria.list{
			between("transactionOn", GasUtil.getFirstDayOfMonth(month), GasUtil.getLastDayOfMonth(month))
			order("transactionOn", "desc")
		}
		if(regularInvList.size() > 0){
			def inventoryRegularInstance = regularInvList.get(0)
			gasSummary.regularInventoryGallons = inventoryRegularInstance.regularCloseGallons
			gasSummary.regularInventoryCost = inventoryRegularInstance.regularOpenInvCost
			gasSummary.regularAvgCost = inventoryRegularInstance.regularAvgCost
			double regularTotalSales = 0
			double regularSalesCost = 0
			for(InventoryRegular inventoryRegular :regularInvList){
				regularTotalSales = regularTotalSales + inventoryRegular.regularSaleGallons
				regularSalesCost = regularSalesCost + inventoryRegular.regularCost
			}
			gasSummary.regularTotalSales = regularTotalSales
			gasSummary.regularSalesCost = regularSalesCost
			gasSummary.regularAvgSalesPrice = regularSalesCost/regularTotalSales
			gasSummary.regularCost = gasSummary.regularAvgCost * gasSummary.regularTotalSales
			gasSummary.regularGrossMargin = gasSummary.regularSalesCost - gasSummary.regularCost
			gasSummary.regularGrossMarginPerGallon = gasSummary.regularGrossMargin / gasSummary.regularTotalSales
		}
		if(midGradeInvList.size() > 0){
			def inventoryMidGradeInstance = midGradeInvList.get(0)
			gasSummary.midGradeInventoryGallons = inventoryMidGradeInstance.midGradeCloseGallons
			gasSummary.midGradeInventoryCost = inventoryMidGradeInstance.midGradeOpenInvCost
			double midGradeTotalSales = 0
			double midGradeSalesCost = 0
			for(InventoryMidGrade inventoryMidGrade :midGradeInvList){
				midGradeTotalSales = midGradeTotalSales + inventoryMidGrade.midGradeSaleGallons
				midGradeSalesCost = midGradeSalesCost + inventoryMidGrade.midGradeCost
			}
			gasSummary.midGradeTotalSales = midGradeTotalSales
			gasSummary.midGradeSalesCost = midGradeSalesCost
			gasSummary.midGradeAvgSalesPrice = midGradeSalesCost/midGradeTotalSales
			gasSummary.midGradeAvgCost = inventoryMidGradeInstance.midGradeAvgCost
			gasSummary.midGradeCost = gasSummary.midGradeAvgCost * gasSummary.midGradeTotalSales
			gasSummary.midGradeGrossMargin = gasSummary.midGradeSalesCost - gasSummary.midGradeCost
			gasSummary.midGradeGrossMarginPerGallon = gasSummary.midGradeGrossMargin / gasSummary.midGradeTotalSales
		}
		if(premiumInvList.size() > 0){
			def inventoryPremiumInstance = premiumInvList.get(0)
			gasSummary.premiumInventoryGallons = inventoryPremiumInstance.premiumCloseGallons
			gasSummary.premiumInventoryCost = inventoryPremiumInstance.premiumOpenInvCost
			double premiumTotalSales = 0
			double premiumSalesCost = 0
			for(InventoryPremium inventoryPremium :premiumInvList){
				premiumTotalSales = premiumTotalSales + inventoryPremium.premiumSaleGallons
				premiumSalesCost = premiumSalesCost + inventoryPremium.premiumCost
			}
			gasSummary.premiumTotalSales = premiumTotalSales
			gasSummary.premiumSalesCost = premiumSalesCost
			gasSummary.premiumAvgSalesPrice = premiumSalesCost/premiumTotalSales
			gasSummary.premiumAvgCost = inventoryPremiumInstance.premiumAvgCost
			gasSummary.premiumCost = gasSummary.premiumAvgCost * gasSummary.premiumTotalSales
			gasSummary.premiumGrossMargin = gasSummary.premiumSalesCost - gasSummary.premiumCost
			gasSummary.premiumGrossMarginPerGallon = gasSummary.premiumGrossMargin / gasSummary.premiumTotalSales
		}

		render(view: "list", model: [gasSummary:gasSummary, monthYear:month])
	}
}
